package com.soccer.soccerclub.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soccer.soccerclub.entity.Commodity;
import com.soccer.soccerclub.service.CommodityService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    /**
     * 前台商品列表
     * @param page
     * @param commodity type 1饰品 2服装
     * @return
     */
    @GetMapping("/list")
    public R list(Page<Commodity> page,Commodity commodity){
        Page<Commodity> page1 = commodityService.page(page, new LambdaQueryWrapper<Commodity>(commodity).eq(Commodity::getStatus,0));
        return R.ok(page1);
    }
    /**
     * 后台商品列表
     * @param page
     * @param commodity type 1饰品 2服装
     * @return
     */
    @GetMapping("/adminList")
    public R adminList(Page<Commodity> page,Commodity commodity){
        Page<Commodity> page1 = commodityService.page(page, new LambdaQueryWrapper<Commodity>(commodity));
        return R.ok(page1);
    }

    /**
     * 添加商品
     * @param commodity
     * @return
     */
    @PostMapping("/add")
    public R addCommodity(@RequestBody Commodity commodity){
        commodity.setCreatTime(LocalDateTime.now());
        boolean save = commodityService.save(commodity);
        return R.ok(save);
    }

    /**
     * 更新商品
     * @param commodity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody Commodity commodity){
        boolean update = commodityService.updateById(commodity);
        return R.ok(update);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(Integer id){
        boolean removeById = commodityService.removeById(id);
        return R.ok(removeById);
    }
}

